03 February 2019
While designing my electro guitar effects pedal (which will be featured in a future post), I had to deal with a fundamental question: How does the shape of a periodic signal affect its harmonic characteristics? Or, more technically speaking, how does the shape of a periodic signal affects its Fourier Transform? To analyze this on a simplified level, I have decided to use MATLAB.
While I am using MATLAB in this case - mostly because I have access
to an educational license - the same procedure can probably also be done
with python
using numpy
and/or
scipy
. My MATLAB version is MATLAB_R2018b.
Since I hate how MATLAB’s subplot()
deals with
whitespace, I have decided to use a user package called Panel
instead. You can download the .zip file and then extract it to somewhere
in your MATLAB path.
I will be analysing four types of sinusoidal waves. The first one will be a pure sine wave with the frequency of 100Hz and amplitude of 2. The second will be the half-wave rectified version of the same wave (so the negative parts will be 0). The third waveform will be a clipped version of the pure sine wave with the clipping occuring at \pm 0.5. The last waveform will be an asymmetrically clipped sine with the clippings at +0.4 and -0.2. The code is as follows:
= 5000;
sampl = 100;
freq
= panel();
p 4,2);
p.pack(
= 0:1/sampl:0.5-1/sampl;
t
= sin(2*pi*freq*t);
fSin = fft(fSin);
fSinFFT = halfRecSin(t,freq);
fHR = fft(fHR);
fHRFFT = clipSin(t,freq,0.5);
fClip = fft(fClip);
fClipFFT = asymClipSin(t,freq,0.5,-0.2);
fAsymClip = fft(fAsymClip);
fAsymClipFFT
= (0:length(fAsymClipFFT)-1)*sampl/length(fAsymClipFFT);
w
% plotting
1,1).select();
p(plot(t,fSin, 'LineWidth',2)
xlim([0, 0.1])
title('normal sine')
1,2).select();
p(plot(w, abs(fSinFFT), 'LineWidth',2)
xlim([5, 750])
title('normal sine FFT')
2,1).select();
p(plot(t,fHR, 'LineWidth',2)
xlim([0, 0.1])
title('half-rect sine')
2,2).select();
p(plot(w,abs(fHRFFT), 'LineWidth',2)
xlim([5, 750])
title('half-rect fft')
3,1).select();
p(plot(t,fClip, 'LineWidth',2)
title('clipped sine')
xlim([0, 0.1])
3,2).select();
p(plot(w,abs(fClipFFT), 'LineWidth',2)
xlim([5, 750])
title('clipped fft')
4,1).select();
p(plot(t,fAsymClip, 'LineWidth',2)
title('asymClipped sine')
xlim([0, 0.1])
4,2).select();
p(plot(w,abs(fAsymClipFFT), 'LineWidth',2)
xlim([5, 750])
title('asymClipped fft')
't11.png', '-w400', '-h200');
p.export(% funcs
function y = halfRecSin(t,freq)
= zeros(length(t));
output for i = 1:length(t)-1
= sin(2*pi*freq*t(i));
fnc if(fnc >= 0)
i) = fnc;
output(else
i) = 0;
output(end
end
= output;
y end
function y = clipSin(t,freq,clip)
= zeros(length(t));
output for i = 1:length(t)-1
= sin(2*pi*freq*t(i));
fnc if(fnc >= clip)
i) = clip;
output(elseif(fnc <= -clip)
i) = -clip;
output(else
i) = fnc;
output(end
end
= output;
y end
function y = asymClipSin(t,freq,clipPlus,clipMinus)
= zeros(length(t));
output for i = 1:length(t)-1
= sin(2*pi*freq*t(i));
fnc if(fnc >= clipPlus)
i) = clipPlus;
output(elseif(fnc <= clipMinus)
i) = clipMinus;
output(else
i) = fnc;
output(end
end
= output;
y end
Here are the resulting plots:
We can see that the asymmetrically clipped sine wave has the most number of harmonic components, with peaks at almost all the multiples of 100. This implies that when a sine wave is asymmetrically clipped, it produces more rich and resonant harmonics. In the terms of an electric guitar, it is better to design your transistor and diode clipping to be asymmetric: it will introduce more harmonics.